package com.bdilab.iot.controller;

import com.bdilab.iot.annotation.CurrentUser;
import com.bdilab.iot.annotation.LogOperation;
import com.bdilab.iot.annotation.LogTag;
import com.bdilab.iot.common.ResultMap;
import com.bdilab.iot.consts.Constants;
import com.bdilab.iot.enums.OperationType;
import com.bdilab.iot.model.Permission;
import com.bdilab.iot.model.User;
import com.bdilab.iot.service.PermissionService;
import com.bdilab.iot.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author WangNing
 * @version 1.0
 * @date 2020/8/1 22:46
 */
@RestController
@Api(tags = "权限管理")
@LogTag(value = "权限管理")
@RequestMapping(value = Constants.BASE_API_PATH + "/permission")
public class PermissionController {

    @Autowired
    TokenUtils tokenUtils;

    @Autowired
    PermissionService permissionService;


    @GetMapping("/all_permission")
    @PreAuthorize("hasPermission('','超级管理员')")
    @ApiOperation("查询系统全部权限")
    @LogOperation(operationName = "查询系统全部权限", operationType = OperationType.SELECT)
    public ResponseEntity getAllPermission(HttpServletRequest request){
        ResultMap resultMap = new ResultMap(tokenUtils);
        Map<String, Map<String, List<Permission>>> permissions = permissionService.getAllPermission();
        resultMap.successAndRefreshToken(request).payload(permissions);
        return ResponseEntity.ok(resultMap);
    }



}
